/*============================================================================*/
/*                   CONTINENTAL AUTOMOTIVE SYSTEMS                           */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* name:            HALStateMachines.c
* version:         1.1
* created_by:      Lizette Acosta/Rodrigo Casados
* date_created:    June 22 2013
*=============================================================================*/
/* DESCRIPTION : State Machines                                               */
/*============================================================================*/
/* FUNCTION COMMENT : main function of Window Lifter                          */
/*                                                                            */
/*============================================================================*/
/*                               OBJECT HISTORY                               */
/*============================================================================*/
/*  REVISION |   DATE      |                               |      AUTHOR      */
/*----------------------------------------------------------------------------*/
/*  1.1      | 22/June/2013 |                              | Lizette Acosta   */
/*                                                           Rodrigo Casados  */
/*============================================================================*/
#include "stdtypedef.h"
#include "HALPorts.h"
#include "HALStateMachines.h"
#include "HALDebouncer.h"


static T_UBYTE sub_LedBar_Percentage;

/*============================================================================*/
/*                            vfn_DefineWinStatus                             */
/*============================================================================*/
void vfn_DefineWinStatus(e_StateFlag e_SelectedMode)
{
	vfn_RegularWinLif(e_SelectedMode);
}


/*============================================================================*/
/*                             vfn_RegularWinLif                              */
/*============================================================================*/
void vfn_RegularWinLif (e_StateFlag e_SelectedMode)
{
	switch(e_SelectedMode)
	{
		case INITIAL:
			sub_LedBar_Percentage = COMP_CLOSED;
			(*ptrToLedEnable[sub_LedBar_Percentage])();
		break;
		
		case TO_DEFINE:
		/* Wait until another movement/change is selected */
		break;
		
		case UP:
		
			if(sub_LedBar_Percentage < COMP_CLOSED)		/* Increments only if the window is not completely closed */
			{
				sub_LedBar_Percentage++;
				(*ptrToLedEnable[sub_LedBar_Percentage])();
			}
			
			else
			{
			}
			
		break;
		
		case DOWN:
			if(sub_LedBar_Percentage > COMP_OPEN)		/* Decrements only if the window is not completely open */
			{
				sub_LedBar_Percentage--;
				(*ptrToLedEnable[sub_LedBar_Percentage])();
			}
			
			else
			{
			}
		
		break;
	}
}


/*============================================================================*/
/*                             vfn_PinchMode                                  */
/*============================================================================*/
void vfn_PinchMode(T_UBYTE * u8p_ptrToPinchFlag)
{
	static T_UBYTE slub_5secCont = 0;
	
	if(sub_LedBar_Percentage > COMP_OPEN)				/* Decrements only if the window is not completely open */
		{
			sub_LedBar_Percentage--;
			(*ptrToLedEnable[sub_LedBar_Percentage])();
		}
		
	else
	{
	}
		
	if(slub_5secCont == 5)
	{
		*u8p_ptrToPinchFlag = 0;		/* Resets Pinch Flag upon 5sec completion*/
	}
	
	else
	{
	}

}



/*============================================================================*/
/*                          vfn_OneTouchFeature                               */
/*============================================================================*/
void vfn_OneTouchFeature(T_UBYTE * u8p_ptrToOneTouchFlag, e_StateFlag lub_Direction)
{

//////////////////////////////////////
// FALTA HACER QUE LA FUNCION SE DETENGA SI HAY BOTONAZO
//////////////////////////////////////

	switch(lub_Direction)
	{
		case UP:		
			if(sub_LedBar_Percentage < COMP_CLOSED)		/* Increments only if the window is not completely closed */
			{
				sub_LedBar_Percentage++;
				(*ptrToLedEnable[sub_LedBar_Percentage])();
			}	
			else
			{
				*u8p_ptrToOneTouchFlag = 0;
			}		
		break;
		
		case DOWN:
			if(sub_LedBar_Percentage > COMP_OPEN)		/* Increments only if the window is not completely closed */
			{
				sub_LedBar_Percentage--;
				(*ptrToLedEnable[sub_LedBar_Percentage])();
			}	
			else
			{
				*u8p_ptrToOneTouchFlag = 0;
			}
		break;
		
		default:
		break;
	}
	

}


/*============================================================================*/
/*                             vfn_ModeSelect                                 */
/*============================================================================*/
e_StateFlag ubfn_ModeSelect (T_UBYTE * u8p_ptrToPinchFlag , T_UBYTE * u8p_ptrToOneTouchFlag)
{
	static e_StateFlag seub_SelectedMode;
	
	vfn_ReadButtons(&seub_SelectedMode);	/* Reads the 3 buttons and writes the ID of the pressed one. 
									         Writes To_DEFINE if there's no event. */
	if(ubfn_Deboouncer(&seub_SelectedMode))
	{
	/* Possible return values of u8fn_Deboouncer function: */
	/* INVALID: When it detects an invalid event */
	/* UP/DOWN/PINCH: Upon succesful detection */	
		switch(seub_SelectedMode)
		{
			case UP:
			case DOWN:
			/* Determines whether it was a single selection, or OneTouch Mode */
			if()//////////// (timer <= 450 ms --> OneTouch)
			{
				*u8p_ptrToOneTouchFlag = TRUE;
			}
			
			else /* Single selection Detected */
			{
				vfn_RegularWinLif (seub_SelectedMode);
			}
			
			break;
			
			case PINCH:
				*u8p_ptrToPinchFlag = TRUE;
			break;
			
			case INVALID:			
				vfn_RegularWinLif (INVALID);
			break;
			
			default:
			break;
		}		
	}
	
	else
	{
	}
		
	return seub_SelectedMode;
}

